<?php

namespace app\api\controller;

use app\api\model\Page;
use app\api\model\Problem;
use app\api\model\Share;
use app\api\model\Video;
use app\common\controller\Api;
use app\api\model\Article as ArticleModel;

class Article extends Api
{
    protected $noNeedLogin = ['Article','ArticleDetails','Page','Video','VideoDetails','Problem'];
    protected $noNeedRight = ['*'];

    public function Article()
    {
        $is_recommend = $this->request->param('is_recommend',0);
        $Article = new ArticleModel();
        $where['is_recommend']=['in',$is_recommend];
        $list=$Article->where($where)->order('weigh desc')->paginate();
        $this->success('',$list);
    }
    public function ArticleDetails()
    {
        $id = $this->request->param('id');
        $Article = new ArticleModel();
        $where['id']=$id;
        $find=$Article->where($where)->find();
        $Article->where($where)->setInc('view');

        $user=$this->auth;
        $Share = new Share();
        $wherese['user_id']=$user->id;
        $wherese['status']=1;
        $wherese['num']=$id;
        $wherese['type_data']=1;
        $Share_find=$Share->where($wherese)->order('id desc')->find();
        if(empty($Share_find)){
            $is_collect=0;
        }else{
            $is_collect=1;
        }

        $find['is_collect']=$is_collect;
        $this->success('',$find);
    }
    public function Page()
    {
        $id = $this->request->param('id');
        $Page = new Page();
        $where['id']=$id;
        $find=$Page->where($where)->find();
        $this->success('',$find);
    }
    public function Problem()
    {
        $Problem = new Problem();
        $list=$Problem->order('weigh desc')->paginate();
        $this->success('',$list);
    }
    public function Video()
    {
        $keyword = $this->request->param('keyword');
        $c_id = $this->request->param('c_id');
        $is_recommend = $this->request->param('is_recommend',0);
        $Video = new Video();
        if(!empty($c_id)){
            $where['category_id']=$c_id;
        }
        if(!empty($keyword)){
            $where['title']=['like','%'.$keyword.'%'];
        }
        $where['is_recommend']=['in',$is_recommend];
        $list=$Video->where($where)->order('weigh desc')->paginate();
        $this->success('',$list);
    }
    public function VideoDetails()
    {
        $id = $this->request->param('id');
        $Video = new Video();
        $where['id']=$id;
        $find=$Video->where($where)->find();
        $Video->where($where)->setInc('view');

        $user=$this->auth;
        $Share = new Share();
        $wherese['user_id']=$user->id;
        $wherese['status']=1;
        $wherese['num']=$id;
        $wherese['type_data']=2;
        $Share_find=$Share->where($wherese)->order('id desc')->find();
        if(empty($Share_find)){
            $is_collect=0;
        }else{
            $is_collect=1;
        }

        $find['is_collect']=$is_collect;
        $this->success('',$find);
    }

    public function Share()
    {
        $user=$this->auth;
        $id = $this->request->param('id');
        $type = $this->request->param('type');
        $status =$this->request->param('status');

        $Share = new Share();
        $where['user_id'] = $user->id;
        $where['num']=$id;
        $where['type_data']=$type;
        $where['status']=$status;
        $find=$Share->where($where)->find();
        if(empty($find)){
            $data['num']=$id;
            $data['user_id']=$user->id;
            $data['type_data']=$type;
            $data['createtime']=time();
            $data['status']=$status;
            $Share->save($data);
        }else{
            if($status==1){
                $Share->where(['id'=>$find['id']])->delete();
            }
        }
        $this->success('','');
    }
}
